/**
 * 
 */
package de.fsujena.chat_pi.ressources;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Observable;

import javax.swing.tree.TreeNode;

import org.jasypt.util.password.*;

/**
 * A class representing a user and storing relevant informations. The password is stored 
 * as a SHA-256-hash for security reasons.
 * 
 * @author Erik Hebestreit
 * @see UserManagemant
 */
public class User implements Serializable, TreeNode {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -4343343581995365352L;
	
	public String name;
	private String passwordHash;
	public List<String> dir=new ArrayList<String>();						//contains positions in the room manager
	public enum ROLE {STANDARD, MODERATOR, ADMIN,GUEST }	
	public ROLE role = ROLE.STANDARD; 
	
	public enum STATUS {NORMAL, BANNED, }
	public STATUS status = STATUS.NORMAL;
	private TreeNode parent;
	private int type;
	// Constants for types of node
    public static final int NODE_ROOM         = 0;
    public static final int NODE_USER     = 1;
    
	/**
	 * initialize a new user with name and password
	 * 
	 * @param name name of the user
	 * @param password password of the user (private)
	 */
	public User(String name,String password) {
		// initialize password encryptor
		StrongPasswordEncryptor passwordEncryptor = new StrongPasswordEncryptor();
		dir.clear();
		this.name = name;
		// save password after converting to a SHA-256-hash
		this.passwordHash = passwordEncryptor.encryptPassword(password);
		type=NODE_USER;
	}
	
	/**
	 * set a new name for the user
	 * 
	 * @param name new name for user
	 * @return <b>true</b> if successful
	 */
	public Boolean setName(String name) {
		this.name = name;
		return true;
	}
	
	/**
	 * set a new password
	 * 
	 * @param oldPassword current password to verify permission of changing password
	 * @param newPassword password to be set
	 * @param newPasswordRetyped retyped new password, to check for typos
	 * @return 0 states a successful reseting of the password, 1 states a wrong old
	 * 			password, 2 states a mismatch between the new password and the retyped 
	 * 			new password
	 */
	public boolean setPassword(String oldPassword,
			String newPassword) {
		// initialize password encryptor
		StrongPasswordEncryptor passwordEncryptor = new StrongPasswordEncryptor();
		
		if(!checkPassword(oldPassword))
			return false; // returns 1 if the old password is not right
		
		this.passwordHash = passwordEncryptor.encryptPassword(newPassword);
		return true;
	}
	
	/**
	 * checks if the input value matches the stored password of the user
	 * 
	 * @param passwordToCheck password to be checked
	 * @return <b>true</b> if password matches the stored one, <b>false</b> if there 
	 * 			is a mismatch
	 */
	public Boolean checkPassword(String passwordToCheck) {
		// initialize password encryptor
		StrongPasswordEncryptor passwordEncryptor = new StrongPasswordEncryptor();
		
		return passwordEncryptor.checkPassword(passwordToCheck, this.passwordHash);
	}
	public List<String> getDir(){
		return this.dir;
	}
	public void setDir(List<String> dir){
		this.dir=dir;
	}
	
	/**
	 * compares if the directory is the same as the saved one
	 * @param dirToCompare the directory to compare
	 * @return true if the same and false if not the same
	 */
	public boolean compareDir(List<String> dirToCompare){
		
		if(dir==null && dirToCompare!=null){
			return false;
		}else if(dir==null && dirToCompare==null){
			return true;
		}else if(dir!=null && dirToCompare==null){
			return false;
		}
		if(this.dir.size()!=dirToCompare.size()){
			return false;
		}
		for(int i=0;i<this.dir.size();i++){
			if(!this.dir.get(i).equals(dirToCompare.get(i))){
				return false;
			}
		}
		return true;
	}

	@Override
	public Enumeration children() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean getAllowsChildren() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public TreeNode getChildAt(int arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int getChildCount() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int getIndex(TreeNode arg0) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public TreeNode getParent() {
		// TODO Auto-generated method stub
		return parent;
	}
	public void setParent(TreeNode parent) {
        this.parent = parent;
    }

	@Override
	public boolean isLeaf() {
		// TODO Auto-generated method stub
		return true;
	}
	
	 public void setTitle(String title) {
	        this.name = title;
	    }

	    public String getTitle() {
	        return name;
	    }
	   
	    /**
	     * The node object should override this method to provide a text that will
	     * be displayed for the node in the tree.
	     */
	    public String toString() {
	        return name;
	    }

	    public int getType() {
	        return type;
	    }   
	
}
